package com.bdqn.mr.combine;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.CombineTextInputFormat;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.input.TextInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.mapreduce.lib.partition.HashPartitioner;

import java.io.IOException;

public class WordCountDriver {
    public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
        // 5.1 获取配置信息以及获取 job 对象
        Configuration conf = new Configuration();
        Job job = Job.getInstance(conf);

        // 5. 2 关联本 Driver 程序的 jar
        job.setJarByClass(WordCountDriver.class);

        job.setPartitionerClass(HashPartitioner.class);


        // 5.3 关联 Mapper 和 Reducer 的 jar
        job.setMapperClass(WordCountMapper.class);
        job.setReducerClass(WordCountReducer.class);

        // 5.4 设置 Mapper 输出的 kv 类型
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(IntWritable.class);

        // 5.5 设置最终输出 kv 类型
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(IntWritable.class);

        // 第一步：切换InputFormat，处理小文件
        job.setInputFormatClass(CombineTextInputFormat.class);

        // 第二步：合并后的切片大小:4M  8M 16M
        CombineTextInputFormat.setMaxInputSplitSize(job, 16 * 1024 * 1024 * 1);


        // 5.6 设置输入和输出路径
        FileInputFormat.setInputPaths(job, new Path("D:\\mr\\combine\\input"));
        FileOutputFormat.setOutputPath(job, new Path("D:\\mr\\combine\\output"));

        // 5. 7 提交 job
        System.exit(job.waitForCompletion(true) ? 0 : 1);
    }
}
